
namespace SB2.Core;

public static class SugarRepositoryExt
{
    /// <summary>
    /// 分页查询数据
    /// </summary>
    /// <typeparam name="Q"></typeparam>
    public static async Task<PagedResponse<Q>> GetPageListAsync<Q>(this ISugarQueryable<Q> queryable, PagedRequest page) where Q : class, new()
    {
        var order = page.GetOrder();
        RefAsync<int> totalCount = 0;
        var data = await queryable
         .Where(page.GetConditions())
         .OrderByIF(!string.IsNullOrEmpty(order), order)
         .ToPageListAsync(page.PageIndex, page.PageSize, totalCount);
        return new PagedResponse<Q>(data, totalCount);
    }
    /// <summary>
    /// 分页数据
    /// </summary>
    public static async Task<PagedResponse> GetPageListAsync<T, Q>(this ISugarQueryable<T> queryable, PagedRequest page) where Q : class, new()
    {
        var dto = DisplayColumnDto.GetDisplayColumnDto<Q>();
        var res = await queryable.Select<Q>(dto.GetSelectFields()).GetPageListAsync(page);
        res.Columns = dto.GetDisplayColumnDtos();
        return res.ToResponse();
    }

}
